package com.jvxb.manage.livable.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jvxb.common.constants.WorkOrderConstants;
import com.jvxb.manage.livable.entity.WorkOrder;
import com.jvxb.manage.livable.vo.ClusterApplyVo;
import com.jvxb.manage.livable.vo.Employee;
import com.jvxb.manage.livable.vo.DataAlterVo;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jvxb
 * @since 2022-01-10
 */
@Component
public class WorkOrderUtil {

    public static List<String> getUserCodeList(String users) {
        List<Employee> employees = JSONArray.parseArray(users, Employee.class);
        return employees.stream().map(e -> e.getUserCode()).collect(Collectors.toList());
    }

    public static WorkOrder createClusterApplyOrder(ClusterApplyVo clusterApplyVo) {
        WorkOrder order = new WorkOrder();
        Employee creator = clusterApplyVo.getCreator();
        String creatorStr = JSON.toJSONString(creator);
        String operateStr = JSON.toJSONString(clusterApplyVo.getOperator());
        Employee operateEmp = JSONObject.parseObject(operateStr, Employee.class);
        order.setCreaterId(Long.valueOf(creator.getUserCode()));
        order.setCreator(creatorStr);
        order.setCreateTime(new Date());
        order.setUpTime(clusterApplyVo.getUpTime());
        order.setTitle(clusterApplyVo.getTitle());
        order.setCreateDesc(clusterApplyVo.getCreateDesc());
        order.setEnv(clusterApplyVo.getEnv());
        order.setApprovers(JSON.toJSONString(clusterApplyVo.getApprovers()));
        order.setReaders(JSON.toJSONString(clusterApplyVo.getReaders()));
        order.setOperator(operateStr);
        order.setExecuteId(Long.valueOf(operateEmp.getUserCode()));
        order.setSubhead(clusterApplyVo.getDbType());
        order.setDbType(clusterApplyVo.getDbType());
        order.setAssignee(order.getApprovers());
        order.setProcessType(WorkOrderConstants.DB_DEPLOY);
        order.setPriority(WorkOrderConstants.NORMAL);
        order.setOrderStatus(WorkOrderConstants.CREATE);
        return order;
    }

    public static WorkOrder createDataAlertOrder(DataAlterVo dataAlterVo) {
        WorkOrder order = new WorkOrder();
        Employee creatorEmp = dataAlterVo.getCreator();
        String creatorStr = JSON.toJSONString(creatorEmp);
        Employee operateEmp = dataAlterVo.getOperator();
        order.setCreaterId(Long.valueOf(creatorEmp.getUserCode()));
        order.setCreator(creatorStr);
        order.setCreateTime(new Date());
        order.setUpTime(dataAlterVo.getUpTime());
        order.setTitle(dataAlterVo.getTitle());
        order.setCreateDesc(dataAlterVo.getCreateDesc());
        order.setClusterId(dataAlterVo.getClusterId());
        order.setEnv(dataAlterVo.getEnv());
        order.setApprovers(JSON.toJSONString(dataAlterVo.getApprovers()));
        order.setReaders(JSON.toJSONString(dataAlterVo.getReaders()));
        order.setOperator(JSON.toJSONString(operateEmp));
        order.setExecuteId(Long.valueOf(operateEmp.getUserCode()));
        order.setSubhead(dataAlterVo.getDbType());
        order.setDbType(dataAlterVo.getDbType());
        order.setAssignee(order.getApprovers());
        order.setProcessType(WorkOrderConstants.DB_DATA_ALERT);
        order.setPriority(WorkOrderConstants.NORMAL);
        order.setOrderStatus(WorkOrderConstants.CREATE);
        return order;
    }
}
